import slack_sdk as slack
from common.logger import logger


def notify(msg: str, sender: str = "S2", level: str = "INFO", printout: bool = False):
    """Send a notification to the team's Slack monitoring channel using a given sender

    Parameters
    ----------
    msg : str
        The message to be sent to the Slack channel.

    sender : str, optional
        The message sender's name.
        If `sender` is one of ['S1', 'S2', 'L8', 'GROWTH', 'TRAINING', 'OTHERS'],
        the message will be sent by the corresponding sender,
        otherwise the message will be sent by the `Others` sender.
        Default is "Others".

    level : str, optional
        Either "INFO" or "ALERT".
        "INFO" messages will sent to the channel "#sat-info"
        "ALERT messages will be sent to the channel ""#sat-alerts""

    printout: bool, optional
        Either "True" or "False".
        "True" not send out slack msg but also print out msg.
        "False" send out slack msg only.
    """

    # Senders are set in the paii.cv.lab@gmail.com Slack account.
    S1 = "xoxb-4088372444279-4115639316369-U9sQ43LK3RUgYwqaiz9PCcTa"
    S2 = "xoxb-4088372444279-4126678196400-wnLh8Glgr2RYbQZRw8tXZfuS"
    L8 = "xoxb-4088372444279-4103040676099-QqJ4UFGWtziDpkaX4ZO82rar"
    GROWTH = "xoxb-4088372444279-4102913328451-i6nCd8aOlxMqJ3DRXUVkcf3l"
    TRAINING = "xoxb-4088372444279-4102963741619-fRZNk09qu3cCUZMKS6CBNtZq"
    OTHERS = "xoxb-4088372444279-4088515989975-vRTiKvivHGReIedI3b0tMIED"

    SLACK_CHANNEL = "#sat-alerts" if level == "ALERT" else "#sat-info"
    if sender.upper() == "S1":
        client = slack.WebClient(token=S1)
    elif sender.upper() == "S2":
        client = slack.WebClient(token=S2)
    elif sender.upper() == "L8":
        client = slack.WebClient(token=L8)
    elif sender.upper() == "GROWTH":
        client = slack.WebClient(token=GROWTH)
    elif sender.upper() == "TRAINING":
        client = slack.WebClient(token=TRAINING)
    else:
        client = slack.WebClient(token=OTHERS)

    try:
        client.chat_postMessage(channel=SLACK_CHANNEL, text=msg)
        if printout:
            logger.info(msg)
    except Exception as err:
        logger.error("Fail to send message because of {}".format(str(err)))
